/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.confiteriofuentes.modelo;

import com.confiteriafuentes.modelo.distribucion.Envio;
import com.mysql.jdbc.PreparedStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 *
 * @author Man
 */
public class EnvioDao {
    
    private Model model;
    private PreparedStatement psInsert;

    public EnvioDao() throws SQLException {
        model = Model.getInstance();
    }



    public void insert(Envio t) throws SQLException {

        
        if (psInsert == null) {
            String sqlInsert = "INSERT INTO envio (placa_camion,fecha) VALUES (?,?)";
            psInsert = (PreparedStatement) model.getCon().prepareStatement(sqlInsert);
        }

       psInsert.setString(1,t.getCamion().getPlaca());
 

       java.sql.Date fecha = new java.sql.Date(t.getFecha().getTime());

       psInsert.setDate(2, fecha);


     


        psInsert.executeUpdate();




    }


     public  int NumRegistros()throws SQLException{
        int numfil=0;
            if (model.getCon() == null ) {
          throw new SQLException(" no hay conexion ");
         }
         ResultSet rs = null;
          PreparedStatement pst = null;
           try {
           pst = (PreparedStatement) model.getCon().prepareStatement("select count(*) AS  numfil from envio");
            rs = pst.executeQuery();

             while(rs.next()) {
             numfil = rs.getInt("numfil");
         }
            }finally {
        if (pst != null) {
             pst.close();
        }

   }
         return numfil;
    }

       public  int IncrementarCod()throws SQLException{
        int cod=0;

            if (model.getCon() == null ) {
          throw new SQLException(" no hay conexion ");
         }
         ResultSet rs = null;
          PreparedStatement pst = null;
           try {
            pst = (PreparedStatement) model.getCon().prepareStatement("select max(codigo_envio)+1 AS codInt from envio");
            rs = pst.executeQuery();

             while(rs.next()) {
             cod= rs.getInt("codInt");
         }
            }finally {
        if (pst != null) {
             pst.close();
        }

   }
         return cod;
    }


        public Envio load(ResultSet rs) throws SQLException {
       CamionDAO cdao= new CamionDAO();
        Envio u = new Envio();
        
        



        u.setCodigo(rs.getString(1));
        u.setCamion(cdao.find(rs.getString(2)));
   
        u.setFecha(rs.getDate(3));

    


        return u;
    }


    public Envio find(Integer id) throws SQLException {

        

                  Connection conex=(Connection) model.getCon();

                if (!model.isConnect())
			throw new SQLException("no hay conexcion");
                Envio pe= new Envio();
		PreparedStatement pstmt=null;
		ResultSet rs=null;

		try{
			pstmt=(PreparedStatement) conex.prepareStatement("SELECT * FROM envio WHERE codigo_envio=?");
			rs=pstmt.executeQuery();
			if(rs.next()){


                              pe=(Envio)load(rs);


                        }else{

                        }
		}finally{
			if(rs!=null)
				rs.close();

			if(pstmt!=null)
				pstmt.close();
		}

		return pe;
    }


    


}
